import QtQuick
import QtQuick.Controls

Item {
    id: view
    height: 252
    width: 1831
    function setCurrentNode(layout:variant, value:int, stopValue:variant){
        let last = value - 1;
        let children = layout.children;
        let i = 0;
        if(stopValue.includes(value - 1)){
            for(i = 0;i < children.length;i++){
                children[i].highlights = false;
                children[i].playAnimation = false;
            }
            return;
        }
        for(i = 0;i < children.length;i++){
            if(i < last){
                children[i].highlights = true;
            }
            else{
                children[i].highlights = false;
            }
            if(last === i){
                children[i].playAnimation = true;
            }
            else{
                children[i].playAnimation = false;
            }
        }
        // let b = 1;
        // let i = 0;
        // let last = -1;
        // let children = layout.children;
        // for(i = 15;i >= 0;i--){
        //     if((b << i) & value){
        //         last = i;
        //         break;
        //     }
        // }
        // for(i = 0;i < children.length;i++){
        //     children[i].playAnimation = false;
        //     children[i].highlights = false;
        // }
        // if(last === -1){
        //     return;
        // }
        // else{
        //     for(i = 0;i < last;i++){
        //         if(i < children.length){
        //             children[i].highlights = true;
        //         }
        //     }
        //     if(last < children.length){
        //         children[last].playAnimation = true;
        //     }
        // }
    }
    Column {
        anchors.fill: parent
        spacing: 10

        Rectangle {
            id: rectangle4
            width: parent.width
            height: 50
            color: "#0018eecd"

            Row {
                id: row1
                anchors.fill: parent
                anchors.leftMargin: 18

                TabText {
                    id: tabText
                    text: "钻杆库"
                    anchors.verticalCenter: parent.verticalCenter
                    checked: false
                }

                Rectangle {
                    id: rectangle8
                    width: parent.width - 105
                    height: parent.height
                    color: "#00ffffff"
                    border.color: "#3d0ef1fa"
                    border.width: 2

                    Row {
                        id: row6
                        height: parent.height
                        spacing: -2

                        StepNode {
                            id: stepNode
                            text: "支撑油缸松"
                            anchors.verticalCenter: parent.verticalCenter
                            playAnimation: false
                            highlights: false
                        }

                        StepNode {
                            id: stepNode1
                            text: "钻杆框升"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode2
                            text: "支撑油缸夹"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode3
                            text: "钻杆框升顶"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode4
                            text: "钻杆框降底"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }
                    }
                }
            }
        }

        Rectangle {
            id: rectangle5
            width: parent.width
            height: 50
            color: "#00621616"

            Row {
                id: row2
                anchors.fill: parent
                anchors.leftMargin: 18

                TabText {
                    id: tabText1
                    text: "运钻杆"
                    anchors.verticalCenter: parent.verticalCenter
                    checked: false
                }

                Rectangle {
                    id: rectangle9
                    width: parent.width - 105
                    height: parent.height
                    color: "#00ffffff"
                    border.color: "#3d0ef1fa"
                    border.width: 2

                    Row {
                        id: row7
                        height: parent.height
                        spacing: -2
                        StepNode {
                            id: stepNode5
                            text: "托钻杆升"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode6
                            text: "托杆钻前移"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode7
                            text: "托杆钻降"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode8
                            text: "托杆钻后移"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode9
                            text: "钻杆右移"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode35
                            text: "等待抓手取钻"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode10
                            text: "钻杆左移"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                    }
                }
            }
        }

        Rectangle {
            id: rectangle6
            width: parent.width
            height: 50
            color: "#00621616"

            Row {
                id: row4
                anchors.fill: parent
                anchors.leftMargin: 18

                TabText {
                    id: tabText2
                    text: "抓钻杆"
                    anchors.verticalCenter: parent.verticalCenter
                    checked: false
                }

                Rectangle {
                    id: rectangle10
                    width: parent.width - 105
                    height: parent.height
                    color: "#00ffffff"
                    border.color: "#3d0ef1fa"
                    border.width: 2

                    Row {
                        id: row8
                        height: parent.height
                        spacing: -2
                        StepNode {
                            id: stepNode11
                            text: "抓手正负旋初始角度"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode12
                            text: "抓手后旋到位置1"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode13
                            text: "抓手伸"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode14
                            text: "抓手前旋到位置2"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode15
                            text: "抓手抓紧"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode16
                            text: "抓手前旋到位置8"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode17
                            text: "抓手前旋到位置4"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode24
                            text: "抓手正负旋机架角度"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }


                        StepNode {
                            id: stepNode18
                            text: "抓手前旋到位置5"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode36
                            text: "等待推进架空"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode19
                            text: "抓手前旋到推进架"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode20
                            text: "后夹持合"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode21
                            text: "抓手松开"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode23
                            text: "抓手后旋到位置4"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }


                    }
                }
            }
        }

        Rectangle {
            id: rectangle7
            width: parent.width
            height: 50
            color: "#00621616"

            Row {
                id: row3
                anchors.fill: parent
                anchors.leftMargin: 18

                TabText {
                    id: tabText3
                    text: "推进"
                    anchors.verticalCenter: parent.verticalCenter
                    checked: false
                }

                Rectangle {
                    id: rectangle11
                    width: parent.width - 105
                    height: parent.height
                    color: "#00ffffff"
                    border.color: "#3d0ef1fa"
                    border.width: 2

                    Row {
                        id: row9
                        height: parent.height
                        spacing: -2
                        StepNode {
                            id: stepNode25
                            text: "动力头正转推进至最前"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode26
                            text: "前夹夹紧"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode27
                            text: "动力头反转卸扣"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode28
                            text: "动力头反转起拨至最后"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode37
                            text: "等待放钻"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode29
                            text: "动力头接后扣"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode30
                            text: "后夹持开"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode31
                            text: "接前扣"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode32
                            text: "前夹持开"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                        StepNode {
                            id: stepNode33
                            text: "通水等待"
                            anchors.verticalCenter: parent.verticalCenter
                            highlights: false
                        }

                    }
                }
            }
        }
    }
    Connections{
        target: attributes

        function onRegisterUpdated(address:int, bit:int, bv:bool){
            switch(address){
            case 100:
                if(attributes.values["100"] !== 0xff){
                    tabText.checked = true;
                }
                else{
                    tabText.checked = false;
                }
                view.setCurrentNode(row6, attributes.values["100"] + 1, [0xff]);
                break;
            case 101:
                if(attributes.values["101"] !== 0xff &&
                        attributes.values["101"] !== 7){
                    tabText1.checked = true;
                }
                else{
                    tabText1.checked = false;
                }
                view.setCurrentNode(row7, attributes.values["101"] + 1, [0xff, 7]);
                break;
            case 102:
                if(attributes.values["102"] !== 0xff){
                    tabText2.checked = true;
                }
                else{
                    tabText2.checked = false;
                }
                view.setCurrentNode(row8, attributes.values["102"] + 1, [0xff]);
                break;
            case 103:
                if(attributes.values["103"] !== 0xff){
                    tabText3.checked = true;
                }
                else{
                    tabText3.checked = false;
                }
                view.setCurrentNode(row9, attributes.values["103"] + 1, [0xff]);
                break;
            }
        }
    }
}
